﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Xml;
using System.Threading;

namespace ImageEditor
{
    public partial class Form1 : Form
    {
        private ArrayList StatesManager = new ArrayList();
        public Form1()
        {
            InitializeComponent();
            this.panel1.Paint += new PaintEventHandler(panel1_Paint);
        }

        private ArrayList GetTimePeriodObjects()
        {
            ArrayList timeList = new ArrayList();
            ArrayList stateList = new ArrayList();
            String[] tagNames = { "timeperiod" };
            String URL = "http://www.cdc.gov/flu/weekly/flureport.xml";
            ArrayList nodeLists = DataManagement.XMLManager.GetXMLNodeLists(URL, tagNames);
            var mainList = nodeLists[0] as XmlNodeList;
            for (int i = 0; i < mainList.Count; i++)
            {
                var State = mainList.Item(i).ChildNodes;
                for (int j = 0; j < State.Count; j++)
                {
                    var StateDataList = State.Item(j).ChildNodes;
                    var symbol = StateDataList.Item(0).InnerText;
                    var activityLevel = StateDataList.Item(1).InnerText;
                    if (!symbol.Equals("PR") && !symbol.Equals("VI") && !symbol.Equals("DC") && !symbol.Equals("GU"))
                    {
                        stateList.Add(new State(symbol, activityLevel));
                    }
                }
                var attributes = mainList.Item(i).Attributes as XmlAttributeCollection;
                var weekNumber = attributes.Item(0).InnerText;
                var year = attributes.Item(1).InnerText;
                var subtitle = attributes.Item(2).InnerText;
                timeList.Add(new TimePeriod(weekNumber, year, subtitle, stateList));
                stateList = new ArrayList();
            }
            return timeList;
        }

        // THE REST OF THE METHODS DEAL WITH GRAPHICS
        void panel1_Paint(object sender, PaintEventArgs e)
        {
            base.OnPaint(e);
            ArrayList timePeriods = GetTimePeriodObjects();
            foreach (TimePeriod period in timePeriods)
            {
                this.Text = period.Subtitle;
                foreach (State state in period.States)
                {
                    Bitmap map = new Bitmap(state.StateImage);
                    map = ChangeColor(map, Color.FromArgb(192, 192, 192), state.Color);
                    e.Graphics.DrawImageUnscaled((Image)map, state.Location);
                    StatesManager.Add(state);
                }
                return;
                Thread.Sleep(250);
            }
        }

        private Bitmap ChangeColor(Bitmap map, Color oldColor, Color newColor)
        {
            for (int x = 0; x < map.Width; x++)
            {
                for (int y = 0; y < map.Height; y++)
                {
                    if (map.GetPixel(x, y).Equals(oldColor))
                    {
                        map.SetPixel(x, y, newColor);
                    }
                }
            }
            return map;
        }

        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
            for (int i = 0; i < 50; i++)
            {
                State state = (State)StatesManager[i];
                if (state.Rectangle.Contains(e.Location))
                {
                    Console.WriteLine(state.Name);
                    break;
                }
            }
        }
    }
}
